package org.adorsys.aderp.cash.work.cashstate;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.adorsys.aderp.cash.domain.CashRegister;
import org.springframework.util.Assert;

/**
 * this his a basic cash register state maintainer. 
 * you can provide yours by implementing CashRegStateMaintainer 
 * and update the implementation in applicationContext-work.xml
 * @author adorsys
 *
 */
public class BasicCashRegStateMaintainer implements CashRegStateMaintainer{
	
	private  List<FullDayCashRegister> fullDayCashRegisters = new ArrayList<FullDayCashRegister>();
	
	@Override
	public void open(FullDayCashRegister fullDayCashRegister) {
		Assert.notNull(fullDayCashRegister, "Null Parameter is not required");
		/*if(this.fullDayCashRegisters.contains(fullDayCashRegister)) return;
		if(CashRegisterState.OPEN.equals(fullDayCashRegister.getCashRegisterState()))return;*/
		System.out.println("Testing cash register to open");
		int index = 0;
		for(FullDayCashRegister fullDayCashReg : this.fullDayCashRegisters){
			if(fullDayCashReg.getCashRegister().getCashRegisterKey().equals(fullDayCashRegister.getCashRegister().getCashRegisterKey())){
				fullDayCashRegister.setCashRegisterState(CashRegisterState.OPEN);
				this.fullDayCashRegisters.set(index, fullDayCashRegister);
				return ;
			}
			index ++;
		}
		fullDayCashRegister.setCashRegisterState(CashRegisterState.OPEN);
		this.fullDayCashRegisters.add(fullDayCashRegister);
	}

	@Override
	public void close(FullDayCashRegister fullDayCashRegister) {
		Assert.notNull(fullDayCashRegister, "Null parameter is not required");
		int index = 0;
		System.out.println("Testing cash register to close");
		for(FullDayCashRegister cashReg : this.fullDayCashRegisters){
			if(cashReg.getCashRegister().getCashRegisterKey().equals(fullDayCashRegister.getCashRegister().getCashRegisterKey())){
				fullDayCashRegister.setCashRegisterState(CashRegisterState.CLOSED);
				System.out.println("Value setted for close  "+fullDayCashRegister.getCashRegisterState());
				this.fullDayCashRegisters.set(index, fullDayCashRegister);
				return;
			}
			index ++;
		}
	}

	@Override
	public void suspend(FullDayCashRegister fullDayCashRegister) {
		Assert.notNull(fullDayCashRegister, "Null parameter is not required");
		int index = 0;
		for(FullDayCashRegister fullCashReg : this.fullDayCashRegisters){
			if(fullCashReg.getCashRegister().getCashRegisterKey().equals(fullDayCashRegister.getCashRegister().getCashRegisterKey())){
				fullDayCashRegister.setCashRegisterState(CashRegisterState.SUSPENDED);
				this.fullDayCashRegisters.set(index, fullDayCashRegister);
				return;
			}
			index ++;
		}
	}
	@Override
	public List<FullDayCashRegister> getFullDayCashRegisters(){
		return this.fullDayCashRegisters;
	}
}